package com.lq.ecoldchain.ui.mine.user.paypassword

import android.arch.lifecycle.MutableLiveData
import com.lq.ecoldchain.base.viewmodel.BaseViewModel
import com.lq.ecoldchain.internet.BaseObserver
import com.lq.ecoldchain.util.MD5
import com.lq.ecoldchain.util.toast

/*
*2019/1/5 0005  11:44
*function by lq
*/
class PayPasswordViewModel : BaseViewModel() {
    val payPhone by lazy { MutableLiveData<String>() }
    val payCode by lazy { MutableLiveData<String>() }
    val payPwd by lazy { MutableLiveData<String>() }


    fun sendPayCode() {
        BaseNetCore.ApiServiceImpl.sendPayPasswordCode(payPhone.value!!).compose(RxTransformer.switchSchedulers()).subscribe(object : BaseObserver<String>() {
            override fun onSuccess(t: String?) {
                toast { "发送成功" }
            }

            override fun onFail(code: String) {
                toast { "发送失败" }
            }
        })
    }

    fun commitPay() {
        if(payPwd.value.isNullOrEmpty()||payCode.value.isNullOrEmpty()){
            toast { "请填写好验证码与支付密码" }
        }else
        BaseNetCore.ApiServiceImpl.setPayPassword(MD5.encode(payPwd.value!!),payCode.value!!).compose(RxTransformer.switchSchedulers()).subscribe(object : BaseObserver<String>() {
            override fun onSuccess(t: String?) {
                if (!t.isNullOrEmpty()) {
                    toast { "设置支付密码成功" }
                } else {
                    toast { "设置支付密码失败" }
                }
            }

            override fun onFail(code: String) {}
        })
    }
}